/*
                    Button Class Implementation
*/
#include "Button.h"

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>

using namespace std;

Button::Button()
{
    Image = NULL;
    ImageClicked = NULL;
    ImageDisabled = NULL;

    Position = Point(0,0);

    iWidth = 0;
    iHeight = 0;

    bClicked = false;
    bVisible = true;
    bEnabled = true;

    strText = "";
}

Button::~Button()
{
    SDL_FreeSurface(Image);
    SDL_FreeSurface(ImageClicked);

    if (ImageDisabled != NULL)
    {
        SDL_FreeSurface(ImageDisabled);
    }
}

int Button::Initialise(Point NewPosition, string strNewText, string strFontPath, int iTextSize, string strFilepath, string strCFilepath, SDL_Color TextColour)
{
    Initialise(NewPosition, strNewText, strFontPath, iTextSize, strFilepath, strCFilepath, strCFilepath, TextColour);
}

int Button::Initialise(Point NewPosition, string strNewText, string strFontPath, int iTextSize, string strFilepath, string strCFilepath, string strDFilepath, SDL_Color TextColour)
{
    strText = strNewText;

    Position = NewPosition;

    Image = OptimiseAlphaSurface(IMG_Load(strFilepath.c_str()));
    ImageClicked = OptimiseAlphaSurface(IMG_Load(strCFilepath.c_str()));
    ImageDisabled = OptimiseAlphaSurface(IMG_Load(strDFilepath.c_str()));

    iWidth = Image->w;
    iHeight = Image->h;

    lblButtonText.Initialise(Position, strFontPath, iTextSize, strText, TextColour);

    Point LabelPosition = Position;

    LabelPosition.x += (Image->w / 2) - (lblButtonText.getWidth() / 2);
    LabelPosition.y += (Image->h / 2) - (lblButtonText.getHeight() / 2);

    lblButtonText.setPosition(LabelPosition);
}

int Button::HandleMouseDown(int iMouseX, int iMouseY)
{
    double xpos = Position.x;
    double ypos = Position.y;

    if((iMouseX > xpos) && (iMouseX < xpos + iWidth) && (iMouseY > ypos) && (iMouseY < ypos + iHeight))
    {
        bClicked = true;

        return 1;
    }

    return 0;
}

int Button::HandleMouseUp(int iMouseX, int iMouseY)
{
    if (bClicked == true)
    {
        bClicked = false;

        return 1;
    }

    return 0;
}

int Button::HandleMouseMotion(int iMouseXREL, int iMouseYREL)
{
    int iMouseX, iMouseY;

    SDL_GetMouseState(&iMouseX, &iMouseY);

    double xpos = Position.x + FramePos.x;
    double ypos = Position.y + FramePos.y;

    if((iMouseX > xpos) && (iMouseX < xpos + iWidth) && (iMouseY > ypos) && (iMouseY < ypos + iHeight))
    {
        return 2;
    }
    else
    {
        bClicked = false;
        return -1;
    }
}

int Button::setImage(string strFilepath)
{
    Image = OptimiseAlphaSurface(IMG_Load(strFilepath.c_str()));

    if (Image != NULL)
    {
        iWidth = Image->w;
        iHeight = Image->h;
    }
}

int Button::setImage(SDL_Surface *newImage)
{
    Image = newImage;

    if (Image != NULL)
    {
        iWidth = Image->w;
        iHeight = Image->h;
    }
}

SDL_Surface* Button::getImage()
{
    return Image;
}

int Button::BlitToSurface(SDL_Surface *screen)
{
    if (bVisible == true)
    {
        SDL_Rect dest;
        dest.x = static_cast<int>(Position.x);
        dest.y = static_cast<int>(Position.y);

        if (bClicked == false)
        {
            SDL_BlitSurface(Image, NULL, screen, &dest);
        }
        else
        {
            SDL_BlitSurface(ImageClicked, NULL, screen, &dest);
        }
    }

    return 0;
}

int Button::BlitToFrame(SDL_Surface *surface, Point FramePosition)
{
    if (bVisible == true)
    {
        SDL_Rect dest;
        dest.x = static_cast<int>(Position.x) + static_cast<int>(FramePosition.x);
        dest.y = static_cast<int>(Position.y) + static_cast<int>(FramePosition.y);

        if (bClicked == true)
        {
            SDL_BlitSurface(ImageClicked, NULL, surface, &dest);
        }
        else if (bEnabled == false)
        {
            SDL_BlitSurface(ImageDisabled, NULL, surface, &dest);
        }
        else
        {
            SDL_BlitSurface(Image, NULL, surface, &dest);
        }

        lblButtonText.BlitToFrame(surface, FramePosition);
    }

    return 0;
}

void Button::setPosition(Point newPosition)
{
     Position = newPosition;
}

void Button::setPosition(double x, double y)
{
     Position.x = x;
     Position.y = y;
}

Point Button::getPosition()
{
       return Position;
}

void Button::setWidth(int iNewWidth)
{
     iWidth = iNewWidth;
}

int Button::getWidth()
{
    return iWidth;
}

void Button::setHeight(int iNewHeight)
{
    iHeight = iNewHeight;
}

int Button::getHeight()
{
    return iHeight;
}

void Button::setText(const string& strNewText)
{
    strText = strNewText;

    lblButtonText.setText(strText);

    Point LabelPosition = Position;

    LabelPosition.x += (Image->w / 2) - (lblButtonText.getWidth() / 2);
    LabelPosition.y += (Image->h / 2) - (lblButtonText.getHeight() / 2);

    lblButtonText.setPosition(LabelPosition);
}

string Button::getText()
{
    return strText;
}
